<template>
  <!-- //评论页面 -->
  <div class="comment">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <!-- :title="item"复制的vant组件title属性删掉，没用会报错 -->
      <!-- 插槽来自定义内容<van-cell><template #title>.. -->
      <van-cell v-for="item in list" :key="item.com_id">
        <template #title>
          <div class="cmt-wrap">
            <van-image round fit="cover" :src="item.aut_photo" />
            <div class="info">
              <div class="name">{{ item.aut_name }}</div>
              <div class="content">{{ item.content }}</div>
              <div class="time">
                {{ item.pubdate | relvTime
                }}<van-button size="mini" @click="showReply(item)"
                  >回复({{ item.reply_count }})</van-button
                >
              </div>
            </div>
            <van-icon
              :name="item.is_liking ? 'like' : 'like-o'"
              class="love"
              @click="loveheart(item)"
              ref="red"
            />
          </div>
        </template>
      </van-cell>
    </van-list>

    <!-- 输入的区域 -->
    <div class="cmt-input-wrap">
      <!-- Field是回复框vant插件 -->
      <van-field
        @keyup.enter="send"
        v-model="cmtValue"
        center
        clearable
        placeholder="请输入评论内容"
      >
        <template #button>
          <van-button @click="send" size="small" type="info">发布</van-button>
        </template>
      </van-field>
    </div>

    <!-- sheet区域 自下弹出回复评论区域 -->
    <van-action-sheet v-model="sheetShow" title="发表回复">
      <reply :currentItem="currentItem" v-if="sheetShow" @sheetShow1="toShow" />
    </van-action-sheet>
  </div>
</template>

<script>
import { cmtListAPI, sendCmtAPI } from "@/api";
import reply from "./reply";
export default {
  components: {
    reply,
  },
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      cmtValue: "",
      offset: undefined, //页面第一次进去没用加载的最后一条评论id，给到undefined

      sheetShow: false, //回复评论
      currentItem: {}, //评论对象数据
      heart: 0,
      // like: "like-o",
    };
  },
  methods: {
    toShow(val) {
      this.sheetShow = val;
    },
    // 点击回复按钮回复评论弹起
    showReply(item) {
      //回复评论 显示
      this.sheetShow = true;
      //将评论对象数据 父传子(把当前被点的评论传递给回复面板)
      this.currentItem = item;
    },

    //评论列表记载
    async onLoad() {
      let res = await cmtListAPI({
        type: "a",
        source: this.$route.query.id, //index页面拿了id了 （一个页面的地址上id可以拿到）
        offset: this.offset,
      });
      this.list.push(...res.data.data.results);
      this.offset = res.data.data.last_id; //在用户下一次下拉刷新前将本次最后一条评论id记录起来，方便请求查找（最后一条id是固定传参值 ）//注意last_id跟results在返回数据里是同级
      this.loading = false;
      if (res.data.data.results.last_id == res.data.data.results.end_id) {
        this.finished = true;
      }
    },
    //点击按钮回复评论
    async send() {
      // 判断非空
      if (this.cmtValue == "") {
        this.$toast("输入不能为空");
        return;
      }
      let res = await sendCmtAPI({
        //文章id
        target: this.$route.query.id,
        //输入评论的内容
        content: this.cmtValue,
      });
      // 服务器会返回添加的这条新的评论
      // 我们只要把评论加到数组里，那么界面就有变化了
      // console.log(res.data.data.new_obj); //里面有aut_id、aut_name、com_id、content、reply_count..
      this.list.unshift(res.data.data.new_obj); //放到评论第一个
      this.$toast("发表评论成功");
      this.cmtValue = "";
    },

    //点赞爱心功能
    loveheart(item) {
      // this.list[love]
      // this.heart = love;

      if (!item.is_liking) {
        item.is_liking = true;
      } else {
        item.is_liking = false;
      }
      // console.log(item.is_liking);
      // console.log(this.list[love]);
      // this.$refs.red.style.background = "red";
    },
  },
};
</script>

<style lang="less" scoped>
.comment {
  margin-top: 30px;
  ::v-deep .cmt-wrap {
    display: flex;
    .van-image {
      width: 45px;
      height: 45px;
    }
    .info {
      margin-left: 20px;
      flex: 1;
      .name {
        color: skyblue;
      }
      .content {
        margin: 8px 0;
        width: 250px;
        word-wrap: break-word;
      }
      .time {
        height: 20px;
        color: #ccc;
        display: flex;
        align-items: center;
        color: purple;
      }
    }
    .love {
      color: red;
    }
  }
  .cmt-input-wrap {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100vw;
  }
}
</style>
